package org.jks.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.jks.model.FlightLine;
import org.springframework.stereotype.Service;

@Service
public class FlightLineService extends EntityService<FlightLine>{
	
	public FlightLineService(){
		MODEL_NAME = FlightLine.class.getSimpleName();
		CLASS_NAME = FlightLine.class;
	}
	
	public FlightLine findByName(String name){
		List<Object> params = new ArrayList<Object>();
		params.add(name);
		return get("from FlightLine where Name=?", params);
	}
	
	@Override
	public void delete(long[] ids){
		Session session = this.openSession();
		
		try{
			session.getTransaction().begin();
			for(long id: ids){
				session.createQuery("delete from TowerCoordinate where FlightLine.Id="+id).executeUpdate();
				session.createQuery("delete from FlightAngle where FlightLine.Id="+id).executeUpdate();
				session.createQuery("delete from FlightLine where Id="+id).executeUpdate();
			}
			session.getTransaction().commit();
		}
		catch(Exception e){
			session.getTransaction().rollback();
		}
		session.flush();
		session.close();
	}
	
	public Serializable save(FlightLine flightLine){
		Session session = this.openSession();
		Serializable obj = null;
		try{
			session.getTransaction().begin();
			obj = session.save(flightLine);
			session.getTransaction().commit();
		}
		catch(Exception e){
			session.getTransaction().rollback();
			
		}
		session.flush();
		session.close();
		
		return obj;
	}
}
